package com.zzti.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import java.util.List;


import com.zzti.Dao.EmployeeDao;
import com.zzti.Thired.Employee;
import com.zzti.Thired.User;
import com.zzti.Uitl.DBuitl;

public class EmployeeDaoimpl implements EmployeeDao{

	@Override
	public List<Employee> getAll() {
		List<Employee> list=new ArrayList<Employee>();
		Connection conn;
		PreparedStatement pst;
		ResultSet rs;
		DBuitl db=new DBuitl();
	String sql="select * from employee";
	
	
		try {
			 conn= DBuitl.getConnection();
		 pst=conn.prepareStatement(sql);
		 rs=pst.executeQuery();
		 while(rs.next()) {    
			 Employee em=new Employee();
			 em.setName( rs.getString(2));
			 em.setSex(rs.getString(3));
			em.setAge(Integer.parseInt(rs.getString(4)));
			em.setDapartment(rs.getString(5));	
			em.setType(rs.getString(6));
			em.setYear(Integer.parseInt(rs.getString(7)));
			
			list.add(em);
				
       }    
		 return list;
		 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public int AddEmployee(Employee em) {
		List<Employee> list=new ArrayList<Employee>();
		int u=1;
		Connection conn;
		PreparedStatement pst;
		ResultSet rs;
		DBuitl db=new DBuitl();
	String sql="insert into employee(name,sex,age,dapartment,type,year) values(?,?,?,?,?,?)";
		try {
			 conn= db.getConnection();
		 pst=conn.prepareStatement(sql);
		 pst.setString(1,em.getName());
			pst.setString(2,em.getSex());
			pst.setInt(3,em.getAge());
			pst.setString(4,em.getDapartment());
			pst.setString(5,em.getType());		
			pst.setInt(6,em.getYear());
		
		u=pst.executeUpdate();
		 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}
	@Override
	public int UpdateEmployee(Employee em,String s) {
		List<Employee> list=new ArrayList<Employee>();
		int u=1;
		Connection conn;
		PreparedStatement pst;
		ResultSet rs;
		DBuitl db=new DBuitl();
	String sql="update   employee set name=?,sex=?,age=?,dapartment=?,type=?,year=? where name=?";
		try {
			 conn= DBuitl.getConnection();
		 pst=conn.prepareStatement(sql);
		 pst.setString(1,em.getName());
			pst.setString(2,em.getSex());
			pst.setInt(3,em.getAge());
			pst.setString(4,em.getDapartment());
			pst.setString(5,em.getType());		
			pst.setInt(6,em.getYear());
			pst.setString(7,s);
		
		u=pst.executeUpdate();
		 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}
	@Override
	public int DeleteEmployee(String em) {
		List<Employee> list=new ArrayList<Employee>();
		int u=1;
		Connection conn;
		PreparedStatement pst;
		ResultSet rs;
		DBuitl db=new DBuitl();
	String sql="delete  from employee where name=?";
		try {
			 conn= DBuitl.getConnection();
		 pst=conn.prepareStatement(sql);
		 pst.setString(1,em);
			
		
		u=pst.executeUpdate();
		 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return u;
	}
}